package CtrLayer;
import DBLayer.*;
import ModelLayer.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 *EmployeeCtr
 *This class handles methods related to employees.
 *31/05/2012
 *@author Bager
 *@version 0.1
 */

public class EventCtr {
	
	private static EventCtr instance;
	
	/**
	 * Creates a new eventCtr.
	 */
	
	private EventCtr()
	{
		
	}
	
	/**
	 * Returns a instance of EventCtr if a EventCtr already exist, creates a new EventCtr and returns it if a EventCtr don't already exist.
	 * @return A instance of EventCtr. 
	 */
	
	public static EventCtr getInstance()
	{
		if(instance==null)
        {
            instance = new EventCtr();
        }
        return instance;
	}
	
	/**
	 * Checks for current events, if there is a VIP1 and a VIP2 event it will return the VIP1 event.
	 * @return A current event if there is any, else null. 
	 */
	
	public Event findEvent()
	{
      Event event2 = null;
	  DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
      DateFormat timeFormat = new SimpleDateFormat("HH");
      Date date = new Date();
	  EventDBIF dbEvent = new EventDB();
	  ArrayList<Event> events = dbEvent.getAllEvents(false);
	  System.out.println("Current dateTime " + dateFormat.format(date) + " " + Integer.parseInt(timeFormat.format(date)));
	  for(Event event : events)
	  {
		  System.out.println("DB Date: " +event.getDate());
		  if(dateFormat.format(date).equals(event.getDate()) && Integer.parseInt(timeFormat.format(date)) >= Integer.parseInt(event.getStartTime()) 
				  && Integer.parseInt(timeFormat.format(date)) < Integer.parseInt(event.getEndTime())
				  )
		  {
			  System.out.println("DateTime: " + event.getDate() + " " + event.getStartTime() + " " + event.getEndTime());
			  event2 = event;
				  
		  }
	  }
	  return event2;
	}
	
	/**
	 * Find events and group them into either a TodaysEvents, UpcomingEvents or RecentEvents collection and return the collection.
	 * 
	 * @param eventStatus The event collection returned, either TodaysEvents, UpcomingEvents or RecentEvents.
	 * @return The chosen collection, depending on your EventStatus parameter. 
	 */
	
	public ArrayList<Event> getEvents(String eventStatus)
	{
		  DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		  DateFormat dayFormat = new SimpleDateFormat("dd");
		  DateFormat monthFormat = new SimpleDateFormat("MM");
		  DateFormat yearFormat = new SimpleDateFormat("yyyy");
	      Date date = new Date();
	      //DateFormat timeFormat = new SimpleDateFormat("HH");
	      //Date time = new Date();
	      EventDBIF dbEvent = new EventDB();
	      ArrayList<Event> events2 = dbEvent.getAllEvents(false); 
	      ArrayList<Event> events = new ArrayList<Event>();
		
		if(eventStatus.equals("TodaysEvents"))
		{
			
			for(Event event : events2)
			{
				if(dateFormat.format(date).equals(event.getDate()))
				{
					events.add(event);
				}
			}
		}
		else if(eventStatus.equals("UpcomingEvents"))
		{
			for(Event event : events2)
			{
				String eventDay = event.getDate().substring(0, 2);
				String eventMonth = event.getDate().substring(3, 5);
				String eventYear = event.getDate().substring(6, 10);
				
				System.out.println(eventDay + " " + eventMonth + " " + eventYear);
				
				if(Integer.parseInt(dayFormat.format(date)) < Integer.parseInt(eventDay) && Integer.parseInt(monthFormat.format(date)) == Integer.parseInt(eventMonth)  || Integer.parseInt(monthFormat.format(date)) < Integer.parseInt(eventMonth) && Integer.parseInt(yearFormat.format(date)) == Integer.parseInt(eventYear) || Integer.parseInt(yearFormat.format(date)) < Integer.parseInt(eventYear))
				{
					events.add(event);
				}
			}
		}
			
			else if(eventStatus.equals("RecentEvents"))
			{
				
				for(Event event : events2)
				{
					String eventDay = event.getDate().substring(0, 2);
					String eventMonth = event.getDate().substring(3, 5);
					String eventYear = event.getDate().substring(6, 10);
					if(Integer.parseInt(dayFormat.format(date)) > Integer.parseInt(eventDay) && Integer.parseInt(monthFormat.format(date)) == Integer.parseInt(eventMonth)  || Integer.parseInt(monthFormat.format(date)) > Integer.parseInt(eventMonth) && Integer.parseInt(yearFormat.format(date)) == Integer.parseInt(eventYear) || Integer.parseInt(yearFormat.format(date)) > Integer.parseInt(eventYear))
					{
						events.add(event);
					}
				}
				
			}
		
		return events;
	}
	
	
}
